import {MemberInterface, MemberOption} from '../utils/types'

export interface EndpointMap {
  [memberId: string]: any
}

export class Member extends MemberInterface {
  protected endpoint: EndpointMap = {};
  public candidatesQueue: any = {};
  public pipeline: any;
  public player: any;
  public recorder: any;
  public shareScreen: any;

  static createMember (options: MemberOption): Member {
    const { userId, username, socketId } = options;
    return new Member({ userId, username, socketId })
  }

  setEndpoint (userId, endpoint) {
    this.endpoint[userId] = endpoint
  }

  getEndpoint (userId) {
    return this.endpoint[userId]
  }

  removeEndpoint (userId) {
    this.endpoint[userId].release()
    delete this.endpoint[userId]
  }

  stop (userId) {
    if (this.endpoint[userId] && this.endpoint[userId].release) {
      this.removeEndpoint(userId)
    }

    if (this.pipeline && this.pipeline.release) {
      if (!userId || userId === this.userId) {
        this.pipeline.release()
        this.pipeline = undefined
      }
    }
  }
}

export interface IMembers {
  [memberId: string]: Member
}

